<template>
  <div class="pc-page">
    <div v-if="loginStatus" class="login_box">
      <div class="title">
        用户登录
      </div>
      <p>用户名</p>
      <van-field v-model="value"  placeholder="请输入用户名" />
      <p style="padding: 10px 0;">请选择头像</p>
      <ul class="avatar">
        <li v-for="(item, index) in list" :class="{checked: currentIndex === index}" :key="index" @click="imgChecked(index)">
          <img :src="item.img" alt="">
        </li>
      </ul>
      <div class="btn-box">
        <van-button type="primary" size="large" @click="login">登录</van-button>
      </div>

    </div>
    <Room
      v-else
      ref="weChat"
      :user="user"
      :userList="userList"
	  :messageContent="messageContent"
	  @upload="upload"
	  @sendMessage="sendMessage"
    ></Room>
  </div>
</template>

<script>
  import io from 'socket.io-client';
  import { Field, Button, Toast } from 'vant';
  import avatar01 from '../../assets/images/avatar01.jpg';
  import avatar02 from '../../assets/images/avatar02.jpg';
  import avatar03 from '../../assets/images/avatar03.jpg';
  import avatar04 from '../../assets/images/avatar04.jpg';
  import avatar05 from '../../assets/images/avatar05.jpg';
  import avatar06 from '../../assets/images/avatar06.jpg';
  import avatar07 from '../../assets/images/avatar07.jpg';
  import avatar08 from '../../assets/images/avatar08.jpg';
  import avatar09 from '../../assets/images/avatar09.jpg';
  import avatar010 from '../../assets/images/avatar010.jpg';
  import Room from "../Room";

  // compositionApi
  export default {
    name: "PC",
    components: {
      [Field.name]: Field,
      [Button.name]: Button,
      Room
      // [Toast.name]: Toast
    },
    data(){
      return{
        currentIndex: 0,
        value: '',
        list: [
          {
            img: avatar01,
          },
          {
            img: avatar02,
          },
          {
            img: avatar03,
          },
          {
            img: avatar04,
          },
          {
            img: avatar05,
          },
          {
            img: avatar06,
          },
          {
            img: avatar07,
          },
          {
            img: avatar08,
          },
          {
            img: avatar09,
          },
          {
            img: avatar010,
          }
        ],
        loginStatus: true,
        http: 'http://localhost:3000/',
        socket: null, // 服务
        user: null, // 用户
        userList: [],
		messageContent: [],
      }
    },
    created() {

      this.socket = io('http://localhost:3000/');
      // 监听登录成功
      this.socket.on('loginSuccess', (data) => {
        this.user = data.data;
        Toast(data.msg);
        this.loginStatus = false;
      });
      // 监听登录失败的请求
      this.socket.on('loginError', (data) =>{
        Toast(data.msg);
      });
      // 监听有用户登录
      this.socket.on('addUser', (data) => {
        console.log('有用户登录', data);
        this.$store.commit('setJoinRoom', data)
      });
      // 更新用户
      this.socket.on('userList', data => {
        this.userList = data;
      });
	  // 收到消息
	  this.socket.on('receiveMessage', data => {
		  // 先测试再进行分类
		  console.log(data);
		  const { content, username, avatar } = data;
		  this.messageContent.push({
		  		type: 'txt',
		  		username,
		  		avatar,
				content
		  })
	  })
	  // 收到图片
	  this.socket.on('receiveImage', data => {
	  		  // 先测试再进行分类
			  const { username,avatar,img } = data ;
			  this.messageContent.push({
				  type: 'img',
				  username,
				  avatar,
				  img
			  })
			
	  })

    },
    mounted() {
      // 监听用户离开
      this.socket.on('delUser', data => {
        console.log('有人离开', data.username);
        this.$store.commit('setLeaveRoom', data);
        console.log(this.$refs.weChat);
        this.$refs.weChat ? this.$refs.weChat.haveOneLeaveRoom() : null
      })

    },
    methods: {
	  upload(data){
		  const username = this.user.username;
		  const avatar = this.user.avatar;
		  const img = data;
		  
		  this.socket.emit('sendImage', {
				username, avatar, img
		  })
	  },
	  sendMessage(data) {
		  console.log(data);
		  this.socket.emit('sendMessage', data);
	  },
      imgChecked(index) {
        this.currentIndex = index;
      },
      login(){
        if (!this.value){
          Toast('请输入名字');
          return;
        }

        let username = this.value;
        let avatar = this.list[this.currentIndex].img;
        // console.log(this.list[this.currentIndex].img);
        // 需要告诉socket io服务，要登录
        // 看服务端能不能接收得到
        this.socket.emit('login', {
          username,
          avatar
        })
      }

    }
  }
</script>

<style scoped lang="scss">
  .van-cell{
    padding: 10px 0 !important;
  }
  .checked{
    border: 1px solid red !important;
  }
  .pc-page{
    width: 100%;
    height: 100%;
    background: url("../../assets/images/bg.jpg") 0 0 no-repeat;
    background-size: 100% 100%;
    ul{
      margin-bottom: 20px;
    }
    .login_box {
      position: absolute;
      top: 40%;
      left: 50%;
      transform: translate(-50%,-40%);
      width: 300px;
      height: 380px;
      padding: 50px;
      background: #fff;
      border-radius: 5px;
      box-shadow: #999 0 2px 10px;
      .title{
        text-align: center;
        font-size: 22px;
      }
      .login_box p {
        font-size: 16px;
        padding: 10px 0;
      }
      .login_box input, .login_box button {
        display: block;
        width: 100%;
        padding: 8px 0;
      }
      .avatar{
        display: flex;
        flex-wrap: wrap;
      }
      .avatar li {
        /*float: left;*/
        /*display: flex;*/
        border: 1px solid transparent;
        width: 60px;
        height: 60px;
        list-style: none;
        padding: 2px;
        box-sizing: border-box;
        img{
          width: 100%;
          height: 100%;
        }
      }

    }
  }
</style>
